﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TFSTibcoDesignerRCSAdapter
{
    public static class LoggingMethods
    {
        /// <summary>
        /// # A method to write errors to the stderr. It is more friendly as it provides context information from the script.
        /// </summary>
        /// <param name="message"></param>
        public static void Logerr(string message)
        {
            Debug(message);
            Console.Error.WriteLine(message);
            Environment.Exit(1);
        }

        /// <summary>
        /// A method to log messages to a log file exists in Tibco Logs Directory \userProfile\.TIBCO\logs\
        /// </summary>
        /// <param name="message"></param>
        public static void Debug(string message)
        {
            var sLogFileName = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) +
                               "\\.TIBCO\\logs\\TibcoRCSAdapterLogFile.txt";

            if (SystemConfig.Default.LogInFile)
            {
                if (!string.IsNullOrEmpty(sLogFileName))
                {
                    using (var file = new System.IO.StreamWriter(sLogFileName, true))
                    {
                        file.WriteLine(message);
                    }
                }
                else
                {
                    Logerr("Log FileName parameter does not exist.");
                }
            }
        }

        public static void CleanUpLogFile()
        {
            var sLogFileName = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) +
                               "\\.TIBCO\\logs\\TibcoRCSAdapterLogFile.txt";
            var file = new System.IO.FileInfo(sLogFileName);
            if (file.Length >= SystemConfig.Default.LogFileSize)                
            {
                using (var newfile = new System.IO.StreamWriter(sLogFileName, false))
                {
                    newfile.WriteLine("New File");
                } 
            }

        }
    }
}
